#include <fstream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <iomanip>

using namespace std;

struct Player
{
	string name;
	string team;
	int score;

	bool operator<( const Player& player ) const
	{
		if( score < player.score )
			return true;
		else if( score == player.score )
		{
			if( name > player.name )
				return true;
			if( name == player.name )
				return team > player.team;
		}
		return false;
	}

	Player& operator+=( int score )
	{
		this->score += score;
		return *this;
	}
};

struct Team
{
	string name;
	int score;

	bool operator<( const Team& team ) const
	{
		if( score < team.score )
			return true;
		else if( score == team.score )
		{
			if( name > team.name )
				return true;
		}
		return false;
	}
};

void main()
{
	ifstream fin( "scores.in" );
	ofstream fout( "scores.out" );

	Player player;
	string team;

	map<string,Player> Playerset;
	map<string,int> Teamset;
	map<string,set<string>> Memberset;
	map<string,Player>::iterator Iter;
	map<string,int>::iterator Iter1;

	set<string>::iterator Iter2;
	set<string> Members;
	priority_queue<Player> Players;
	priority_queue<Team> Teams;
	

	char TorP;

	fin >> TorP;
	while( !fin.eof() )
	{
		fin.get();
		if( TorP == 'T' )
			getline( fin, team );
			
		if( TorP == 'P' )
		{
			player.team = team;
			getline( fin, player.name, ':' );
			fin >> player.score;
			Teamset[player.team] += player.score;
			if( Playerset.count( player.name+player.team ) )
				Playerset[player.name+player.team] += player.score;
			else
			{
				Playerset[player.name+player.team] = player;
				Members = Memberset[player.team];
				Members.insert( player.name+player.team );
				Memberset[player.team] = Members;
			}
		}
		
		fin >> TorP;
	}

	fout << "The best players are:" << endl;

	for( Iter = Playerset.begin(); Iter != Playerset.end(); Iter++ )
		Players.push( (*Iter).second );

	int rank = 1;
	int tie = 0;
	while( !Players.empty() && rank <= 10 )
	{
		player = Players.top();
		Players.pop();

		fout << setw(3) << rank << "  " << left << setw(30) << player.name << setw(30) << player.team << right << setw(5) << player.score << endl;

		if( !Players.empty() && player.score > Players.top().score )
		{
			rank = rank + tie + 1;
			tie = 0;
		}
		else
			tie++;
	}
	while( !Players.empty() )
		Players.pop();

	fout << endl << endl;

	fout << "The best teams are:" << endl;
	rank = 1;
	tie = 0;
	Team teams;
	for( Iter1 = Teamset.begin(); Iter1 != Teamset.end(); Iter1++ )
	{
		teams.name = (*Iter1).first;
		teams.score = (*Iter1).second;
		Teams.push( teams );
	}

	while( !Teams.empty() && rank <= 10 )
	{
		teams = Teams.top();
		Teams.pop();

		fout << setw(3) << rank << "  " << left << setw(30) << teams.name << right << setw(5) << teams.score << endl;
		
		int prank = 1;
		int ptie = 0;
		
		Members = Memberset[teams.name];
		for( Iter2 = Members.begin(); Iter2 != Members.end(); Iter2++ )
		{
			string key = *Iter2;
			player = Playerset[key];
			Players.push( player );
		}

		while( !Players.empty() && prank <= 3 )
		{
			player = Players.top();
			Players.pop();

			fout << "     " << setw(3) << prank << "  " << left << setw(30) << player.name << right << setw(5) << player.score << endl;

			if( !Players.empty() && player.score > Players.top().score )
			{
				prank = prank + ptie + 1;
				ptie = 0;
			}
			else
				ptie++;
		}
		while( !Players.empty() )
			Players.pop();

		if( !Teams.empty() && teams.score > Teams.top().score )
		{
			rank = rank + tie + 1;
			tie = 0;
		}
		else
			tie++;

	}
	fin.close();
	fout.close();
}